"""
⑨ 分别统计性别为男/女的人年龄平均值
"""
import pymysql
from pymysql.cursors import DictCursor

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='6666',
    database='homework'
)
try:
    cursor=conn.cursor(DictCursor)
    sql = """
        select * from student
    """
    ret_num = cursor.execute(sql)
    result = cursor.fetchall()
    # print(result)

    male_age_total=0
    female_age_total=0
    male = 0
    female = 0
    for stu in result:
        if stu['Sex']== '男':
            male_age_total += stu['age']
            male += 1
        else:
            female_age_total += stu['age']
            female += 1

    print("male:", male/male_age_total)
    print("female:", female/female_age_total)
except:
    conn.rollback()

